package com.neighbor.neighborhoodadmin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neighbor.neighborhoodadmin.pojo.MallProducts;
import com.neighbor.neighborhoodadmin.service.IMallProductsService;
import com.neighbor.neighborhoodadmin.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 <p>
 积分商城商品表 前端控制器
 </p>

 @author song
  * @since 2025-09-22 09:11:23
 */

@RestController
@RequestMapping("/mallProducts")
public class MallProductsController {

    @Autowired
    private IMallProductsService productsService;

    @GetMapping("/list")
    public ResponseMsg list(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer size,
                            @RequestParam(required = false) Integer typeId,
                            @RequestParam(required = false) String keyword) {
        Page<MallProducts> pageParam = new Page<>(page, size);
        QueryWrapper<MallProducts> queryWrapper = new QueryWrapper<>();

        if (typeId != null) {
            queryWrapper.eq("type_id", typeId);
        }
        if (keyword != null && !keyword.trim().isEmpty()) {
            queryWrapper.like("name", keyword);
        }
        queryWrapper.orderByDesc("created_at");

        Page<MallProducts> result = productsService.page(pageParam, queryWrapper);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "查询成功", result);
    }

    @GetMapping("/detail/{id}")
    public ResponseMsg detail(@PathVariable Integer id) {
        MallProducts product = productsService.getById(id);
        return product != null ? ResponseMsg.getInstance(ResponseMsg.SUCCESS, "查询成功", product) :
                ResponseMsg.getInstance(ResponseMsg.DATA_ERROR, "商品不存在");
    }

    @PostMapping("/add")
    public ResponseMsg add(@RequestBody MallProducts product) {
        boolean success = productsService.save(product);
        return success ? ResponseMsg.success("添加成功") :
                ResponseMsg.getInstance(ResponseMsg.DATA_ERROR, "添加失败");
    }

    @PutMapping("/update")
    public ResponseMsg update(@RequestBody MallProducts product) {
        boolean success = productsService.updateById(product);
        return success ? ResponseMsg.success("更新成功") :
                ResponseMsg.getInstance(ResponseMsg.DATA_ERROR, "更新失败");
    }

    @DeleteMapping("/delete/{id}")
    public ResponseMsg delete(@PathVariable Integer id) {
        if (id == null) {
            return ResponseMsg.getInstance(ResponseMsg.DATA_ERROR, "商品ID不能为空");
        }

        try {
            // 检查商品是否存在
            MallProducts product = productsService.getById(id);
            if (product == null) {
                return ResponseMsg.getInstance(ResponseMsg.DATA_ERROR, "商品不存在");
            }

            // 物理删除
            boolean success = productsService.removeById(id);

            if (success) {
                return ResponseMsg.success("删除成功");
            } else {
                return ResponseMsg.getInstance(ResponseMsg.DATA_ERROR, "删除失败");
            }
        } catch (Exception e) {
            return ResponseMsg.getInstance(ResponseMsg.DATA_ERROR,
                    "删除过程中发生错误: " + e.getMessage());
        }
    }

    @PutMapping("/updateStatus/{id}")
    public ResponseMsg updateStatus(@PathVariable Integer id, @RequestParam Integer status) {
        MallProducts product = new MallProducts();
        product.setProductId(id);
        product.setStatus(status);
        boolean success = productsService.updateById(product);
        return success ? ResponseMsg.success("状态更新成功") :
                ResponseMsg.getInstance(ResponseMsg.DATA_ERROR, "状态更新失败");
    }
}
